<template>
    <component
      is="a-modal"
      v-model:visible="visible"
      :width="600"
      :title="title"
      :mask-closable="false"
      :ok-loading="loading"
      @cancel="close"
      @before-ok="submit">
      <!-- 表单信息 start -->
      <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
        <a-row :gutter="10">
          <a-col :span="24">
            <a-form-item label="发货时间" field="delivery_time">
              {{ tool.dateFormat(new Date) }}
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发货备注" field="delivery_remark">
              <a-textarea v-model="formData.delivery_remark" placeholder="请输入发货备注" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表单信息 end -->
    </component>
  </template>
  
  <script setup>
  import { ref, reactive, computed } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import api from '../../api/order/order'
  import tool from '@/utils/tool'
  
  const emit = defineEmits(['success'])
  // 引用定义
  const visible = ref(false)
  const loading = ref(false)
  const formRef = ref()
  
  let title = computed(() => {
    return '订单发货'
  })
  
  // 表单信息
  const formData = reactive({
    id: null,
    delivery_remark: '',
  })
  
  // 验证规则
  const rules = {
    delivery_remark: [{ required: true, message: '发货备注必需填写' }],
  }
  
  // 打开弹框
  const open = async (id) => {
    visible.value = true
    formData.id = id
    formData.delivery_remark = ''
    await initPage()
  }
  
  // 初始化页面数据
  const initPage = async () => {
  }
  
  // 数据保存
  const submit = async (done) => {
    const validate = await formRef.value?.validate()
    if (!validate) {
      loading.value = true
      let data = { ...formData }
      let result = await api.delivery(data.id, data)
      if (result.code === 200) {
        Message.success('操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    }
    done(false)
  }
  
  // 关闭弹窗
  const close = () => (visible.value = false)
  
  defineExpose({ open })
  </script>
  